package org.medicine.service.serviceImpl;

import org.medicine.base.DaoSupport;
import org.medicine.model.Medicine;
import org.medicine.service.MedicineService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class MedicineServiceImpl extends DaoSupport<Medicine> implements MedicineService {

    public List<Medicine> allMedicine() {
        Query query = em.createQuery("from Medicine m");
        return query.getResultList();
    }

    public void deleteAllMedicine() {
        Query query = em.createQuery("delete from Medicine");
        query.executeUpdate();
    }

    public Medicine findMedicineByName(String medicineName) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("medicineName", medicineName);
        return this.findSpecialObject(Medicine.class, params);
    }

    public List<Medicine> findMedicines(String value) {
        Query query = em.createQuery("from Medicine m where m.medicineName like ?1 or m.medicineEnglishName like ?1 or m.adaptation like ?1 or m.sort like ?1 ");
        query.setParameter(1, "%" + value + "%");
        return query.getResultList();
    }

}
